//
//  YQLVisitorView.swift
//  YQL_WeiBo
//
//  Created by apple on 2016/11/13.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

//定义一个代理
@objc protocol YQLVisitorViewDelegata {
    
    @objc func userLogin()
    
    @objc func userRegister()
    
}

class YQLVisitorView: UIView {
    
    weak var delegate: YQLVisitorViewDelegata?

    //重写init方法,搭建UI界面
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //提示使用者如果使用Xib或者SB会报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //
    
    //提供一个设置视图的方法
    func setupVisitorView(title: String, imageName: String, isHome: Bool = false) {
        
        titleLabel.text = title
        animationImageView.image = UIImage(named: imageName)
        if isHome {
            animation()
        }else{
            //如果不是首页就隐藏小房子和动画遮罩视图
            centerImageView.isHidden = true
            backImageView.isHidden = true
        }
        
    }
    
    //动画方法
    private func animation() {
        
        let bascAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置动画
        bascAnimation.toValue = M_PI * 2
        bascAnimation.duration = 20
        bascAnimation.repeatCount = MAXFLOAT
        //设置动画完成之后不从视图移除
        bascAnimation.isRemovedOnCompletion = false
        
        animationImageView.layer.add(bascAnimation, forKey: "animation")
    }
    
    
    private func setupUI() {
        
        addSubview(animationImageView)
        addSubview(backImageView)
        addSubview(centerImageView)
        addSubview(titleLabel)
        addSubview(logInButton)
        addSubview(registerButton)
        
        animationImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        centerImageView.snp.makeConstraints { (make) in
            make.center.equalTo(animationImageView)
        }
        backImageView.snp.makeConstraints { (make) in
            make.center.equalTo(animationImageView)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(animationImageView.snp.bottom).offset(50)
            make.centerX.equalTo(self)
        }
        logInButton.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.right.equalTo(self.snp.centerX).offset(-20)
//            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
        }
        registerButton.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.left.equalTo(self.snp.centerX).offset(20)
//            make.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
        }
        
        //给登录按钮和注册按钮添加点击事件
        logInButton.addTarget(self, action: #selector(loginButtonDidClick), for: .touchUpInside)
        
        registerButton.addTarget(self, action: #selector(registerButtonDidClick), for: .touchUpInside)
        //设置视图背景颜色
        self.backgroundColor = UIColor.init(white: 237.0/255.0, alpha: 1)
    }
    
    
    @objc private func loginButtonDidClick() {
        print("登录按钮被点击了")
        delegate?.userLogin()
    }
    @objc private func registerButtonDidClick() {
        print("注册按钮被点击了")
        delegate?.userRegister()
    }
    
    //动画图片
    private lazy var animationImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //主题图片
    private lazy var centerImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //动画遮罩
    private lazy var backImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //标题文字
    private lazy var titleLabel: UILabel = {
        
        let label = UILabel(title: "关注一些人,回到这里看看有什么惊喜", textColor: UIColor.darkGray, textFont: UIFont.systemFont(ofSize: 14))
        
        label.numberOfLines = 0
        label.textAlignment = .center
        label.preferredMaxLayoutWidth = 220
        
        return label
    }()
    //登录按钮
    private lazy var logInButton: UIButton = {
        
        let button = UIButton(title: "登录", textColor: UIColor.darkGray, textFont: UIFont.systemFont(ofSize: 16), imageName: "common_button_white")
    
        return button
        
    }()
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(title: "注册", textColor: UIColor.orange, textFont: UIFont.systemFont(ofSize: 16), imageName: "common_button_white")
        
        return button
        
    }()
    

    
    
    
}




















